package com.tools.developer.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOperation {

	/**
	 * 将指定的某个路径的文件拷贝出来
	 * 
	 * @return
	 */
	public static boolean copy() {

		File directory = new File(
				"/data/data/com.ijinshan.kbatterydoctor/databases/");

		if (directory.isDirectory()) {
			File[] files = directory.listFiles();
			FileOutputStream fileOut = null;
			FileInputStream fileIn = null;
			byte[] buffer = new byte[1024];
			if (files != null) {
				for (File f : files) {
					try {
						fileIn = new FileInputStream(f);
						File sd = new File("/mnt/sdcard/" + f.getName());
						fileOut = new FileOutputStream(sd);
						while (true) {
							int length = fileIn.read(buffer);
							if (length <= 0) {
								break;
							}
							fileOut.write(buffer, 0, length);

						}
						fileOut.flush();
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						if (fileOut != null) {
							try {
								fileOut.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						if (fileIn != null) {
							try {
								fileIn.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}

		}
		return false;
	}

}
